package me.youline.dataServer.controller;

import javax.servlet.http.HttpServletResponse;

import me.youline.dataServer.ConfigConstants;
import me.youline.dataServer.controller.dto.IdPaginationDTO;
import me.youline.dataServer.controller.dto.notify.SocialActionNotifyMsgListDTO;
import me.youline.dataServer.entity.UserToken;
import me.youline.dataServer.service.LatestNotifyRecordService;
import me.youline.dataServer.service.SocialActionNotifyMsgService;
import me.youline.dataServer.service.UserDeviceTokenService;
import me.youline.dataServer.service.authentication.UserTokenService;
import me.youline.dataServer.service.front.IdPageDTOAdapter;
import me.youline.dataServer.service.front.SocialActionNotifyMsgDTOAdapter;
import me.youline.dataServer.service.support.MsgBuilder;
import me.youline.dataServer.utils.ResponseTypeOutputUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 社交行为消息通知模块前端数据控制器
 * 
 * @author linhan
 *
 */
@RequestMapping("/socialActionNotify")
@Controller
public class SocialActionEventNotifyController {
	
	private Logger logger = LoggerFactory.getLogger(SocialActionEventNotifyController.class);
	
	@Autowired
	private SocialActionNotifyMsgDTOAdapter socialActionNotifyMsgDTOAdapter;
	
	@Autowired
	private SocialActionNotifyMsgService notifyMsgService;
	
	@Autowired
	private LatestNotifyRecordService latestNotifyRecordService;
	
	@Autowired
	private UserTokenService userTokenService;
	
	@Autowired
	private UserDeviceTokenService userDeviceTokenService;
	
	/**
	 * 获取社交行为通知信息分页列表
	 * 
	 * @param userId   用户ID
	 * @param appToken 用户token信息
	 * @param pageDTO  分页查询信息
	 * @param response
	 */
	@RequestMapping(value = "/{userId}/list",method = RequestMethod.GET)
	public void getMsgList(@PathVariable Long userId,
			@RequestParam(value = ConfigConstants.REQ_PARAM_APP_TOKEN_NAME) String appToken,@ModelAttribute IdPaginationDTO pageDTO,HttpServletResponse response){
		try {
			UserToken token = userTokenService.getUserTokenByTokenStr(appToken);
			SocialActionNotifyMsgListDTO msgListDTO = socialActionNotifyMsgDTOAdapter.getNotifyMsgList(token.getUserId(), IdPageDTOAdapter.getPagination(pageDTO));
			response.setStatus(HttpServletResponse.SC_OK);
			ResponseTypeOutputUtils.renderJson(response, MsgBuilder.success().addObj("msgList", msgListDTO).toJSONString());
			return;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			ResponseTypeOutputUtils.renderJson( response,MsgBuilder.error().errorCode("200501").errorMsg("读取通知消息列表失败").toJSONString());
			return;
		}
	}
	
	
	 
	
	/**
	 * 判断当前用户是否存在最新通知消息。
	 * 
	 * @param userId 用户ID
	 * @param appToken 消息Token
	 * @param response 
	 */
	@RequestMapping(value = "/{userId}/latestRecord",method = RequestMethod.GET)
	public void isExistsLatestMsgRecord(@PathVariable Long userId,
			@RequestParam(value = ConfigConstants.REQ_PARAM_APP_TOKEN_NAME) String appToken,HttpServletResponse response){
		try {
			UserToken token = userTokenService.getUserTokenByTokenStr(appToken);
			boolean isExistLatestMsg = latestNotifyRecordService.isExistLatestMsg(token.getUserId());
			response.setStatus(HttpServletResponse.SC_OK);
			ResponseTypeOutputUtils.renderJson(response, MsgBuilder.success().addObj("isExistLatestMsg", isExistLatestMsg).toJSONString());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			ResponseTypeOutputUtils.renderJson( response,MsgBuilder.error().errorCode("200502").errorMsg("判断是否有最新通知消息执行失败").toJSONString());
			return;
		}
	}
	
	/**
	 * 上传用户设备token
	 * 
	 * @param appToken 用户身份token
	 * @param deviceToken 设备token
	 * @param response
	 */
	@RequestMapping(value = "/deviceToken",method = RequestMethod.POST)
	public void uploadDeviceToken(@RequestParam(value = ConfigConstants.REQ_PARAM_APP_TOKEN_NAME) String appToken, @RequestParam String deviceToken, HttpServletResponse response){
		try {
			UserToken token = userTokenService.getUserTokenByTokenStr(appToken);
			userDeviceTokenService.updateUserDeviceToken(token.getUserId(), deviceToken);
			response.setStatus(HttpServletResponse.SC_OK);
			ResponseTypeOutputUtils.renderJson(response, MsgBuilder.success().toJSONString());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			ResponseTypeOutputUtils.renderJson( response,MsgBuilder.error().errorCode("200503").errorMsg("用户设备token上传失败").toJSONString());
			return;
		}
	}
	
	
	
}
